package classLoading.clinit;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class DeadLoopInit {
    static {
        System.out.println(Thread.currentThread().getName() + " init DeadLoopInit");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("DeadLoopInit init completed");
    }
}

public class DeadLoopInitDemo {
    final static int N = 5;
    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(N);
        for (int i = 0; i < N; i++) {
            service.execute(() -> {
                System.out.println(Thread.currentThread().getName() + " start");
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                new DeadLoopInit();
                System.out.println(Thread.currentThread().getName() + " exit");
            });
            latch.countDown();
        }
        service.shutdown();
    }
}
